package com.acoffee.utils;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author acoffee
 * @create 2021-11-20 17:31
 */
public class String2Date implements Converter<String, Date> {
    @Override
    public Date convert(String source) {
        if ("".equals(source) || source == null) {
            return null;
        }
        System.out.println("传入的源内容：" + source);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy/MM/dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
        try {
            Date parse =
                    simpleDateFormat.parse(source);
            System.out.println(parse);
            return parse;
        } catch (ParseException e) {

            try {
                Date parse =
                        simpleDateFormat1.parse(source);
                return parse;
            } catch (ParseException parseException) {
                try {
                    Date parse = simpleDateFormat2.parse(source);
                    return parse;
                } catch (ParseException exception) {
                    exception.printStackTrace();
                    System.err.println("格式太变态，我也转换不了了");
                }
            }
        }
        return null;

    }
}
